﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class PCBA_PCBAVTest_PCBAVTestAdd : System.Web.UI.Page
{
    private PCBAVTest pvt;
    private DataRow dr;
    private DataTable pvtdt
    {
        get
        {
            return (DataTable)ViewState["pvtdt"];
        }
        set
        {
            ViewState["pvtdt"] = value;
        }
    }

    public PCBA_PCBAVTest_PCBAVTestAdd()
    {
        pvt = new PCBAVTest();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pvtdt = new DataTable();
            pvtdt.Columns.Add(new DataColumn("TPID", Type.GetType("System.String")));
            pvtdt.Columns.Add(new DataColumn("TPValue", Type.GetType("System.Decimal")));
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        dr = pvtdt.NewRow();
        dr["TPID"] = this.TextBox2.Text.Trim();
        dr["TPValue"] = decimal.Parse(this.TextBox3.Text.Trim());
        if (pvtdt.Select("TPID = '" + this.TextBox2.Text.Trim() + "'").Length == 0)
        {
            pvtdt.Rows.InsertAt(dr, 0);
            Bindams();
            this.TextBox2.Text = "";
            this.TextBox3.Text = "";
            this.TextBox2.Focus();
        }
        else
        {
            string str = "已经在列表中";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }

    protected void Bindams()
    {
        this.GridView1.DataSource = pvtdt;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        pvtdt.Select("TPID = '" + GridView1.Rows[e.RowIndex].Cells[0].Text + "'" )[0].Delete();
        Bindams();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (pvtdt.Rows.Count < 1)
        {
            string str = "请先添加静态电压测试信息!";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
        }
        else
        {
            string pCBSN = this.TextBox1.Text.Trim();
            DateTime dt = DateTime.Now;
            PCBAVTestInfo pvti = new PCBAVTestInfo(pCBSN, dt, User.Identity.Name, this.DropDownList1.SelectedItem.Text);
            if (pvt.PCBAVTest_Insert(pvti, pvtdt))
            {
                this.TextBox1.Text = "";
                pvtdt.Clear();
                Bindams();
                this.TextBox1.Focus();
                string str = pCBSN + " 提交成功!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
            else
            {
                string str = "提交失败，请联系相关人员!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }
}
